package fitness

import (
	"gorm.io/gorm"
	"organoid_public/domain/fitness/model"
	"organoid_public/domain/fitness/respository"
	"organoid_public/infrastructure/db"
)

type MerchantRepository struct {
	db *gorm.DB
}

func NewMerchantRepository(db db.DefaultDb) respository.MerchantRepoInterface {
	return &MerchantRepository{
		db: db,
	}
}

/*
*
根据id获取商户信息
*/
func (m *MerchantRepository) GetMerchantInfoById(id int) *model.Merchant {
	var merchant model.Merchant
	err := m.db.Model(&model.Merchant{}).Where("id = ?", id).Find(&merchant).Error
	if err != nil {
		return nil
	}
	return &merchant
}

/*
*
根据OpenMerchantId获取商户信息
*/
func (m *MerchantRepository) GetMerchantInfoByOpenMerchantId(OpenMerchantId string) *model.Merchant {
	var merchant model.Merchant
	err := m.db.Model(&model.Merchant{}).Where("open_merchant_id = ?", OpenMerchantId).Find(&merchant).Error
	if err != nil {
		return nil
	}
	return &merchant
}

/*
*
编辑商户信息
*/
func (m *MerchantRepository) UpdateMerchant(repo *db.BaseRepo) error {
	//var merchant model.Merchant
	err := m.db.Model(&model.Merchant{}).Where(repo.Where).Updates(repo.UpdateData).Error
	if err != nil {
		return err
	}
	return nil
}

/*
*
新增商户信息
*/
func (m *MerchantRepository) InsertMerchant(merchant *model.Merchant) error {
	//var merchant model.Merchant
	err := m.db.Model(&model.Merchant{}).Create(merchant).Error
	if err != nil {
		return err
	}
	return nil
}
